<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入组织名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--表数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="社团名称" prop="cname">
            <el-input
              v-model="queryParams.cname"
              placeholder="请输入社团名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['manage:community:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="collegeScoreList" @selection-change="handleSelectionChange">
          <el-table-column label="组织名称" align="center" prop="deptName" />
          <el-table-column label="社团名称" align="center" prop="communityName" />
          <el-table-column label="活动名称" align="center" prop="actName" />
          <el-table-column label="得分情况" align="center" prop="activityScore" >
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>总分值: 10 </p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.activityScore }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="获奖情况" align="center" prop="actResultName" />
          <el-table-column label="参赛学生" align="center" prop="userName" />
          <el-table-column label="指导老师" align="center" prop="tname" />
          <el-table-column label="活动状态" align="center" prop="actState">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.actState ==1">进行中</el-tag>
              <el-tag type="success" v-if="scope.row.actState ==2">已结束</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="开始时间" align="center" prop="startTime" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.startTime}}</span>
            </template>
          </el-table-column>
          <el-table-column label="结束时间" align="center" prop="endTime" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.endTime }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getListById"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {exportCommunity } from "@/api/manage/community";
import { getAllActResult, getCollegeActResult } from "@/api/manage/activity";
import { communityTreeSelect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "Community",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动结果数据
      collegeScoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined
      },
      // 表单参数
      form: {},
      // 组织名称
      deptName: undefined,
      // 组织树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      presidentOptions:[],
    };
  },
  watch: {
    // 根据名称筛选组织树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {

    /** 查询组织下拉树结构 */
    getTreeselect() {
      communityTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getListById();
    },
    /** 查询学院得分 */
    getList() {
      this.loading = true;
      getAllActResult(this.queryParams).then(response => {
        debugger
        this.collegeScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询学院得分根据学院ID */
    getListById() {
      this.loading = true;
      getCollegeActResult(this.queryParams).then(response => {
        this.collegeScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有社团数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCommunity(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
